package com.medical.data.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.data.constEnum.CommonEnum;
import com.medical.data.domain.FollowUpPatient;
import com.medical.data.domain.Patient;
import com.medical.data.domain.User;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.FollowUpPatientMapper;
import com.medical.data.mapper.PatientMapper;
import com.medical.data.service.FollowUpPatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.service.PatientService;
import com.medical.data.service.TokenSerivce;
import com.medical.data.utils.DozerUtils;
import com.medical.data.utils.ResultCode;
import com.medical.data.vo.FollowUpPatientVo;
import com.medical.data.vo.LoginUser;
import com.medical.data.vo.OpenFollowUpVo;
import com.medical.data.vo.PatientVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-06
 */
@Service
public class FollowUpPatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements FollowUpPatientService {
    @Autowired
    private TokenSerivce tokenSerivce;

    @Override
    public List<FollowUpPatientVo> getPatient(FollowUpPatientVo followUpPatientVo) {
        User user = tokenSerivce.getUser();
        String userId=user.getId();

        LambdaQueryWrapper<Patient>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getDoctorId,userId)
                .eq(Patient::getIsFollowUp,true);


/*        if ((followUpPatientVo.getBeginTime() == null && followUpPatientVo.getEndTime() != null) ||
                (followUpPatientVo.getBeginTime() != null && followUpPatientVo.getEndTime() == null)) {
            throw new MyException(ResultCode.ERROR.getCode(), "beginTime和endTime必须同时为空或同时不为空");
        }*/

        if (StringUtils.isNotBlank(followUpPatientVo.getProjectId())) {
            wrapper.eq(Patient::getProjectId, followUpPatientVo.getProjectId());
        }



/*        if (followUpPatientVo.getBeginTime() != null) {
            wrapper.ge(Patient::getCreateTime, followUpPatientVo.getBeginTime());
        }

        if (followUpPatientVo.getEndTime() != null) {
            wrapper.le(Patient::getCreateTime, followUpPatientVo.getEndTime());
        }*/

        if (StringUtils.isNotBlank(followUpPatientVo.getPatientName())) {
            wrapper.like(Patient::getPatientName, followUpPatientVo.getPatientName());
        }

        if (StringUtils.isNotBlank(followUpPatientVo.getPatientIdCard())) {
            wrapper.eq(Patient::getIdCard, followUpPatientVo.getPatientIdCard());
        }

        List<Patient> patients = this.baseMapper.selectList(wrapper);

        // 计算出每个对象的具体时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<FollowUpPatientVo>patientVos=new ArrayList<>();
        for (Patient patient : patients) {
/*            Date followUpTime = patient.getFollowUpTime();
            String followUpTimeJson = patient.getFollowUpTimeJson();
            String[] daysArray = followUpTimeJson.split(",");

            List<Date> newFollowUpTimes = new ArrayList<>();
            for (String days : daysArray) {
                int daysToAdd = Integer.parseInt(days);
                long timeInMillis = followUpTime.getTime() + daysToAdd * 24 * 60 * 60 * 1000;
                Date newFollowUpTime = new Date(timeInMillis);
                newFollowUpTimes.add(newFollowUpTime);
            }*/

            // 将计算得到的具体时间存储在对象中
            FollowUpPatientVo followUpPatientVo1 = new FollowUpPatientVo();
            followUpPatientVo1.setProjectId(patient.getProjectId());
            followUpPatientVo1.setPatientName(patient.getPatientName());
            followUpPatientVo1.setPatientIdCard(patient.getIdCard());
            followUpPatientVo1.setNewFollowUpTimes(patient.getNewFollowUpTimes());

            patientVos.add(followUpPatientVo1);
        }

        return patientVos;
    }

    //随访日历的方法，根据时间段，返回随访数量和具体患者
    @Override
    public Map<String, Object> getNumber(FollowUpPatientVo followUpPatientVo) {
        Map<String,Object>map=new HashMap<>();
        return map;
    }

/*    @Override
    public void addFollowPatient(OpenFollowUpVo openFollowUpVo) {
        Patient patient = patientService.getPatientById(openFollowUpVo.getPatientId());
        FollowUpPatient followUpPatient=new FollowUpPatient();
        BeanUtils.copyProperties(patient,followUpPatient);
        //分离时间插入数据到表中
        String times=patient.getNewFollowUpTimes();//patient.getNewFollowUpTimes()=2024-03-21,2024-03-15,2024-03-24,2024-03-19,2024-03-28
        String[] timesArray = times.split(",");
        for(String time : timesArray){
            followUpPatient.setFollowUpTime(time);
            this.baseMapper.insert(followUpPatient);
        }
    }*/

}
